package com.qianfeng.qlm.handforireader.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.qianfeng.qlm.handforireader.R;
import com.qianfeng.qlm.handforireader.model.ActiveBean;
import com.qianfeng.qlm.handforireader.util.BitmapHelp;
import com.qianfeng.qlm.handforireader.util.Constant;

import java.util.List;

/**
 * Created by LinXiangLin on 2015/3/26.
 */
public class ActiveListRecyclerAdapter  extends RecyclerView.Adapter<ActiveListRecyclerAdapter.ViewHolder>{

    private List<ActiveBean> data;
    private Context context;
    private View.OnClickListener listener;

    public ActiveListRecyclerAdapter( Context context,List<ActiveBean> data,View.OnClickListener listener) {
        this.data = data;
        this.context = context;
        BitmapHelp.initUtils(context);
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_active_list, parent, false);
        view.setOnClickListener(listener);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        if (data != null && data.size() > 0){
            ActiveBean bean = data.get(position);
            if (bean != null){
                String activeName = bean.getActive_name();
                String picUrl = bean.getActive_pic_url_1();

                holder.txtTitle.setText(activeName);
                if (!picUrl.startsWith("http://") && !picUrl.startsWith("https://")){

                    picUrl = Constant.URL_SUFFIX + picUrl;
                }

                BitmapHelp.getUtils().display(holder.imgIcon,picUrl,new BitmapLoadCallBack<ImageView>() {
                    @Override
                    public void onLoadCompleted(ImageView imageView, String s, Bitmap bitmap, BitmapDisplayConfig bitmapDisplayConfig, BitmapLoadFrom bitmapLoadFrom) {

                        imageView.setImageBitmap(bitmap);
                    }

                    @Override
                    public void onLoadFailed(ImageView imageView, String s, Drawable drawable) {

                        imageView.setImageDrawable(drawable);
                    }
                });


            }
        }
    }

    @Override
    public int getItemCount() {
        int ret = 0;
        if (data != null && data.size() > 0){
            ret = data.size();
        }
        return ret;
    }

    public void setData(List<ActiveBean> data){
        if (data != null && data.size() > 0){
            this.data.clear();
            this.data.addAll(data);
            notifyItemRangeInserted(0,data.size());
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        private ImageView imgIcon ;
        private TextView txtTitle;
        public ViewHolder(View itemView) {
            super(itemView);
            imgIcon = (ImageView) itemView.findViewById(R.id.item_active_img);
            txtTitle = (TextView) itemView.findViewById(R.id.item_active_title);
        }
    }
}
